package com.ruoyi.web.controller.sujian;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.system.domain.ProperFee;
import com.ruoyi.system.domain.SjElecCost;
import com.ruoyi.system.domain.SysFloor;
import com.ruoyi.system.domain.TenantInfo;
import com.ruoyi.system.service.ISysFloorService;
import com.ruoyi.system.service.ISysProperFeeService;
import com.ruoyi.system.service.ITenantInfoService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 物管费Controller
 *
 * @author zmz
 * @date 2021-07-05
 */
@Controller
@RequestMapping("/sujian/propertyManagementFee")
public class PropertyManFeeController extends BaseController
{
    private String prefix = "propertys/sujian/propertyManagementFee";

    @Autowired
    private ISysProperFeeService SysProperFeeService;

    @Autowired
    private ISysFloorService sysFloorService;

    @Autowired
    private ITenantInfoService tenantInfoService;

    @GetMapping()
    public String propertyManFee(ModelMap modelMap)
    {
        SysFloor sysFloor = new SysFloor();
        List<SysFloor> sysFloorList = sysFloorService.selectSysFloorListAll(sysFloor);
        modelMap.put("sysFloorList",sysFloorList);
        TenantInfo tenantInfo = new TenantInfo();
        List<TenantInfo> tenantInfoList = tenantInfoService.selectTenantInfoList(tenantInfo);
        modelMap.put("tenantInfoList",tenantInfoList);
        return prefix + "/main";
    }
    /**
     * 查询物管费
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ProperFee properFee)
    {
        startPage();
        List<ProperFee> list = SysProperFeeService.selectProperFeeList(properFee);
            return getDataTable(list);

    }


    /**
     * 计算物管费信息
     */
    @GetMapping("/calculate")
    public String calculate(ModelMap mmap)
    {
        return prefix + "/calculate";
    }

    /**
     * 保存计算生成的物管费信息
     */
//    @RequiresPermissions("sujian:propertyManagementFee:calculate")
    @Log(title = "管理费", businessType = BusinessType.INSERT)
    @PostMapping("/calculateValue")
    @ResponseBody
    public AjaxResult calculateValue(HttpServletRequest request, HttpServletResponse resp)
    {
        ProperFee properFeeTmp = new ProperFee();
        SysFloor sysFloor = new SysFloor();
        TenantInfo tenantInfo = new TenantInfo();
        String etldt = request.getParameter("etldt");
        properFeeTmp.setEtldt(etldt);
        List<ProperFee> properFeeList =  SysProperFeeService.selectProperFeeList(properFeeTmp);
        if (properFeeList!=null){
            //删除之前数据
          SysProperFeeService.deleteProperFeeByEtldt(etldt);
        }
        properFeeList = SysProperFeeService.selectProperFeeList(properFeeTmp);
        List<TenantInfo> tenantInfoList = tenantInfoService.selectTenantInfoList(tenantInfo);
//        List<ProperFee> properFeeList = new ArrayList<ProperFee>();
        for(int i = 0;i<tenantInfoList.size();i++){
            ProperFee properFee = new ProperFee();
            sysFloor = sysFloorService.selectSysFloorByName(tenantInfoList.get(i).getTenantFloor());
            properFee.setProperId(UUID.randomUUID().toString().replace("-",""));
            properFee.setNum(tenantInfoList.get(i).getSerialNumber());
            properFee.setCompanyName(tenantInfoList.get(i).getTenantName());
            properFee.setCompanyId(tenantInfoList.get(i).getTenantId());
            properFee.setFloorName(tenantInfoList.get(i).getTenantFloor());
            properFee.setCoveredArea((tenantInfoList.get(i).getTenantArea()==null)?"0.00":tenantInfoList.get(i).getTenantArea().toString());
            String emptyArea = (tenantInfoList.get(i).getEmptyArea()==null)?"0.00":tenantInfoList.get(i).getEmptyArea().toString();
            properFee.setEmptyArea(emptyArea);
            BigDecimal tenantArea = new BigDecimal("0.00");
            if(tenantInfoList.get(i).getTenantArea()!=null){
                tenantArea = tenantInfoList.get(i).getTenantArea();
            }
            String propertyFee = "";
            String propertyEmptyFee = "";
            //增加判断是否收取物业费
            String charges = tenantInfoList.get(i).getCharges();
            if(charges.contains("0")){
                if(StringUtils.isEmpty(sysFloor.getEmptyParam())){
                    sysFloor.setEmptyParam("0");
                }
                propertyFee=tenantArea.multiply(
                        new BigDecimal(sysFloor.getPropertyManaParam())).multiply(
                                new BigDecimal(tenantInfoList.get(i).getChargeCoefficient().toString())).toString();
                propertyEmptyFee=new BigDecimal(emptyArea).multiply(
                        new BigDecimal(sysFloor.getPropertyManaParam())).multiply(
                                new BigDecimal(sysFloor.getEmptyParam())).multiply(
                                        new BigDecimal(tenantInfoList.get(i).getChargeCoefficient().toString())).toString();
            }else{
                propertyFee="0.00";
                propertyEmptyFee="0.00";
            }
            DecimalFormat df = new DecimalFormat("0.00");
            properFee.setPropertyFee(StringUtils.isBlank(propertyFee)?"0.00" :df.format(Double.valueOf(propertyFee)));
            properFee.setPropertyEmptyFee(StringUtils.isBlank(propertyEmptyFee)?"0.00":df.format(Double.valueOf(propertyEmptyFee)));
            properFee.setAreaSum(tenantArea.add(new BigDecimal(emptyArea)).toString());
            properFee.setPropertySum(df.format(Double.valueOf(properFee.getPropertyFee())+Double.valueOf(properFee.getPropertyEmptyFee())));
            properFee.setUpdatetime(new Date());
            properFee.setEtldt(etldt);
            properFeeList.add(properFee);
        }
        return toAjax(SysProperFeeService.insertProperFees(properFeeList));

    }

    /**
     * 修改物管费信息
     */
    @GetMapping("/edit/{properId}")
    public String edit(@PathVariable("properId") String properId, ModelMap mmap)
    {
        System.out.println(properId);
        ProperFee properFee = SysProperFeeService.selectProperFeeById(properId);
        mmap.put("properFee", properFee);
        return prefix + "/edit";
    }

    /**
     * 修改物管费基础信息
     */
//    @RequiresPermissions("sujian:propertyManagementFee:edit")
    @Log(title = "物管费信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ProperFee properFee)
    {
        properFee.setUpdatetime(new Date());
        return toAjax(SysProperFeeService.updateProperFee(properFee));
    }

    /**
     * 删除物管费信息
     */
//    @RequiresPermissions("sujian:propertyManagementFee:remove")
    @Log(title = "物管费信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String[] ids)
    {
        return toAjax(SysProperFeeService.deleteProperFeeByIds(ids));
    }

    /**
     * 导出物管费信息列表
     */
//    @RequiresPermissions("sujian:propertyManagementFee:export")
    @Log(title = "物管费信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(String etldt)
    {
//        String[] floorIds = Convert.toStrArray(etldt);
        List<ProperFee> list = SysProperFeeService.selectProperFeeListByEtldt(etldt);
        ProperFee properFeeTmp = new ProperFee();
        properFeeTmp.setFloorName("合计");
        String jzmj_sum = "0.00";
        String wgf_sum = "0.00";
        String kgmj_sum = "0.00";
        String kgwgf_sum = "0.00";
        String mjxj_sum = "0.00";
        String wgfxj_sum = "0.00";
        for(int i =0;i<list.size();i++){
            jzmj_sum = new BigDecimal(jzmj_sum).add(new BigDecimal(list.get(i).getCoveredArea())).toString();
            wgf_sum = new BigDecimal(wgf_sum).add(new BigDecimal(list.get(i).getPropertyFee())).toString();
            kgmj_sum = new BigDecimal(kgmj_sum).add(new BigDecimal(list.get(i).getEmptyArea())).toString();
            kgwgf_sum = new BigDecimal(kgwgf_sum).add(new BigDecimal(list.get(i).getPropertyEmptyFee())).toString();
            mjxj_sum = new BigDecimal(mjxj_sum).add(new BigDecimal(list.get(i).getAreaSum())).toString();
            wgfxj_sum = new BigDecimal(wgfxj_sum).add(new BigDecimal(list.get(i).getPropertySum())).toString();
        }
        properFeeTmp.setCoveredArea(jzmj_sum);
        properFeeTmp.setPropertyFee(wgf_sum);
        properFeeTmp.setEmptyArea(kgmj_sum);
        properFeeTmp.setPropertyEmptyFee(kgwgf_sum);
        properFeeTmp.setAreaSum(mjxj_sum);
        properFeeTmp.setPropertySum(wgfxj_sum);

        list.add(properFeeTmp);

        ExcelUtil<ProperFee> util = new ExcelUtil<ProperFee>(ProperFee.class);
        return util.exportExcel(list, "物管费");
    }


    /**
     * 导入中的下载模板
     * */
//    @RequiresPermissions("sujian:propertyManagementFee:view")
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<ProperFee> util = new ExcelUtil<ProperFee>(ProperFee.class);
        return util.importTemplateExcel("物管费模板");
    }

    /**
     * 导入物管费列表
     * */
    @Log(title = "物管费", businessType = BusinessType.IMPORT)
//    @RequiresPermissions("sujian:propertyManagementFee:import")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importExcel(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<ProperFee> util = new ExcelUtil<ProperFee>(ProperFee.class);
        List<ProperFee> lists = util.importExcel(file.getInputStream());
        SysUser sysUser = ShiroUtils.getSysUser();
        String message =SysProperFeeService.importProperFee(lists,updateSupport,sysUser);
        return AjaxResult.success(message);
    }
}
